﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Common;
using YiLong.Framework.DataAccess.Config;
using System.Data;
using Dapper;

namespace YiLong.Framework.DataAccess
{
    public class DataCommand
    {
        private DataCommandConfig _dataCommandConfig;
        private DbHelper _dbHelper;
        internal DataCommand(string providerName, string connectionString, DataCommandConfig dataCommandConfig)
        {
            _dataCommandConfig = dataCommandConfig;
            _dbHelper = new DbHelper(providerName, connectionString);
            CommandText = _dataCommandConfig.CommandText;
        }

        public DataCommandConfig DataCommandConfig
        {
            get
            {
                return _dataCommandConfig;
            }
        }

        public string CommandText { get; set; }

        public int ExecuteNonQuery(object param = null, IDbTransaction transaction = null)
        {
            var conn = _dbHelper.GetOpenedConnection();
            return conn.Execute(this.CommandText, param, transaction, _dataCommandConfig.Timeout, _dataCommandConfig.CommandType);
        }

        public object ExecuteScalar(object param = null, IDbTransaction transaction = null)
        {
            var conn = _dbHelper.GetOpenedConnection();
            return conn.ExecuteScalar(_dataCommandConfig.CommandText, param, transaction, _dataCommandConfig.Timeout, _dataCommandConfig.CommandType);
        }


    }
}
